﻿public class Solution {
    public IList<int> PartitionLabels(string s)
    {
        int[] endIndex = new int[26];

        for (int i = s.Length - 1; i >= 0; i--)
        {
            endIndex[s[i] - 'a'] = endIndex[s[i] - 'a'] == 0 ? i : endIndex[s[i] - 'a'];
        }

        IList<int> result = new List<int>();
        int end = -1;
        int max = 0;
        for (int i = 0; i < s.Length; i++)
        {
            if (max < endIndex[s[i] - 'a'])
            {
                max = endIndex[s[i] - 'a'];
            }

            if (i == max)
            {
                result.Add(i - end);
                end = i;
            }
        }

        return result;
    }

    public static void Main(string[] args)
    {
        string s = "ababcbacadefegdehijhklij";
        Solution solution = new Solution();
        IList<int> result = solution.PartitionLabels(s);

        foreach (int res in result)
        {
            Console.Write($"{res} ");
        }
    }
}